import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2025-07-15
 * Time: 23:24
 */
public class Test54 {
    public static boolean canFinish(int numCourses, int[][] prerequisites) {
        Map<Integer, List<Integer>> hash = new HashMap<>();
        // 入度数组
        int[] in = new int[numCourses];
        for(int i = 0; i < prerequisites.length; i++) {
            int a = prerequisites[i][0], b = prerequisites[i][1];
            if(!hash.containsKey(b)) {
                hash.put(b,new ArrayList<>());
            }
            hash.get(b).add(a);
            in[a]++;
        }

        Queue<Integer> q = new LinkedList<>();

        for(int i = 0; i < numCourses; i++) {
            if(in[i] == 0) {
                q.offer(i);
            }
        }
        // 删除边
        while(!q.isEmpty()) {
            int t = q.poll();
            for(int a : hash.getOrDefault(t,new ArrayList<>())) {
                in[a]--;
                if(in[a] == 0) {
                  q.offer(a);
                }
            }
        }

        // 判断是否有环
        for(int i = 0; i < numCourses; i++) {
            if(in[i] != 0) {
                return false;
            }
        }

        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int numCourses = sc.nextInt();
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] prerequisites = new int[n][m];
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                prerequisites[i][j] = sc.nextInt();
            }
        }
        boolean ret = canFinish(numCourses,prerequisites);
        System.out.println(ret);
    }
}
